<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>组件的data为什么要是函数</title>
</head>

<body>
<div id="app">
    <h2>data不使用函数</h2>
    <cpn1></cpn1>
    <cpn1></cpn1>
    <hr>
    <h2>data使用函数</h2>
    <cpn2></cpn2>
    <cpn2></cpn2>
    <hr>
    <h2>data使用函数,cpn3内部在使用方法调用data</h2>
    <cpn3></cpn3>
    <cpn3></cpn3>
    <hr>
    <h2>data使用函数,cpn4内部方法不会和cpn3重复</h2>
    <cpn4></cpn4>
    <cpn4></cpn4>
    <hr>
</div>
<script src="js/vue.min.js"></script>
<template id="cpn1">
    <div>
        <button @click="count--">-</button>
        当前计数：{{count}}
        <button @click="count++">+</button>
    </div>
</template>
<template id="cpn2">
    <div>
        <button @click="count--">-</button>
        当前计数：{{count}}
        <button @click="count++">+</button>
    </div>
</template>
<template id="cpn3">
    <div>
        <button @click="decrement">-</button>
        当前计数：{{count}}
        <button @click="increment">+</button>
    </div>
</template>
<template id="cpn4">
    <div>
        <button @click="decrement">-</button>
        当前计数：{{count}}
        <button @click="increment">+</button>
    </div>
</template>
<script>
    const obj = {
        count:0
    };
    const app = new Vue({
        el: "#app",
        components: { //局部组件创建
            cpn1: {
                template: '#cpn1',
                data() {
                    return obj;//对象
                }
            },
            cpn2: {
                template: '#cpn2',
                data() {
                    return {  //函数
                        count: 0
                    }
                }
            },
            cpn3: {
                template: '#cpn3',
                data() {
                    return {  //函数
                        count: 0
                    }
                },
                methods: {
                    increment() {
                        this.count++;
                    },
                    decrement() {
                        this.count--;
                    }
                }
            },
            cpn4: {
                    template: '#cpn4',
                    data() {
                        return {  //函数
                            count: 0
                        }
                    },
                    methods:{
                        increment(){
                            this.count++;
                        },
                        decrement(){
                            this.count--;
                        }
                    }
            }
        }
    })
</script>
</body>

</html>